package hu.veilingsite.gui;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.client.Systeem;
import hu.veilingsite.shared.ClientUser;

import java.sql.Date;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;

public class EditProfile {
	private Label lblName,lblAddress,lblResidence,lblBirthDate,lblEmail,lbrEmail,lblCredits,lblMayOffer,lblMayBid,lbrMayOffer,lbrMayBid,lbrCredits,lblPassword1,lblPassword2;
	private TextBox tbrName,tbrAddress,tbrResidence,tbrPassword1,tbrPassword2,boxbirthdateday,boxbirthdatemonth,boxbirthdateyear;
	private Button save;
	private ClientUser user = Systeem.ingelogd;
	
	final ServiceAsync myService = (ServiceAsync)
	GWT.create(Service.class);
	final AsyncCallback<ClientUser> callback = new AsyncCallback<ClientUser>() {
		@Override
		public void onFailure(Throwable caught) {
		}
		@Override
		public void onSuccess(ClientUser result) {
			if(result.getId() != 0) {
				//success
				user = result;
			} else {
				System.out.println("er is iets mis gegaan bij het ophalen van uw gegevens probeer het opnieuw.");
			}
		}
	};
	final AsyncCallback<Boolean> saveData = new AsyncCallback<Boolean>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("er is iets mis gegaan bij het maken met een verbinding naar de server, probeer het later opnieuw");
		}
		@Override
		public void onSuccess(Boolean result) {
			if (result) {
				Window.alert("Uw gegevens zijn succesvol opgeslagen.");
			} else {
				Window.alert("Er ging iets mis bij het opslaan van uw gegevens probeer het opnieuw.");
			}
		}
	};
	
	public EditProfile() {
		//update ClientUser
		myService.getUser(user.getEmail(),callback);
	}
	
	public Panel createPage() {
		//Panels
		HorizontalPanel p = new HorizontalPanel();
		HorizontalPanel p4 = new HorizontalPanel();
		//Labels
		lblName = new Label("Naam");
		tbrName = new TextBox();
		lblAddress = new Label("Adres");
		tbrAddress = new TextBox();
		lblResidence = new Label("Woonplaats");
		tbrResidence = new TextBox();
		lblBirthDate = new Label("Geboorte Datum");
		boxbirthdateday = new TextBox();
		boxbirthdateday.setVisibleLength(2);
		boxbirthdateday.addStyleName("sadbox");
		boxbirthdatemonth = new TextBox();
		boxbirthdatemonth.setVisibleLength(2);
		boxbirthdatemonth.addStyleName("sadbox");
		boxbirthdateyear = new TextBox();
		boxbirthdateyear.setVisibleLength(2);
		boxbirthdateyear.addStyleName("sadbox");
		lblEmail = new Label("Email");
		lbrEmail = new Label();
		lblCredits = new Label("Credits");
		lbrCredits = new Label();
		lblMayOffer = new Label("Mag veilen");
		lbrMayOffer = new Label();
		lblMayBid = new Label("Mag bieden");
		lbrMayBid = new Label();
		lblPassword1 = new Label("Nieuw Wachtwoord");
		tbrPassword1 = new TextBox();
		lblPassword2 = new Label("Herhaal Wachtwoord");
		tbrPassword2 = new TextBox();
		save = new Button("Opslaan");
		
		//save User
		save.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				if (!tbrPassword1.getText().equals(tbrPassword2.getText())) {
					System.out.println("wachtwoorden zijn niet gelijk");
				} else {
					user.setAddress(tbrAddress.getText());
					user.setName(tbrName.getText());
					user.setResidence(tbrResidence.getText());
					user.setBirthdate(new Date(Integer.parseInt(boxbirthdateyear.getText())-1900,(Integer.parseInt(boxbirthdatemonth.getText())-1),Integer.parseInt(boxbirthdateday.getText())));
					if (!tbrPassword1.getText().equals("")) {
						myService.updatePassword(tbrPassword1.getText(),user.getEmail(),saveData);
					}
					myService.updateUser(user,saveData);
					Systeem.ingelogd = user;
				}
			}
		});
		//adding to panels
		p4.add(boxbirthdateday);
		p4.add(boxbirthdatemonth);
		p4.add(boxbirthdateyear);
		
		FlexTable table = new FlexTable();
		table.addStyleName("nicetable");
		
		table.setWidget(0,0,lblName);
		table.setWidget(0,1,tbrName);
		table.setWidget(1,0,lblAddress);
		table.setWidget(1,1,tbrAddress);
		table.setWidget(2,0,lblResidence);
		table.setWidget(2,1,tbrResidence);
		table.setWidget(3,0,lblBirthDate);
		table.setWidget(3,1,p4);
		table.setWidget(4,0,lblEmail);
		table.setWidget(4,1,lbrEmail);
		table.setWidget(5,0,lblCredits);
		table.setWidget(5,1,lbrCredits);
		table.setWidget(6,0,lblMayOffer);
		table.setWidget(6,1,lbrMayOffer);
		table.setWidget(7,0,lblMayBid);
		table.setWidget(7,1,lbrMayBid);
		table.setWidget(8,0,lblPassword1);
		table.setWidget(8,1,tbrPassword1);
		table.setWidget(9,0,lblPassword2);
		table.setWidget(9,1,tbrPassword2);
		table.setWidget(10,0,save);
		
		//adding data
		tbrName.setText(user.getName());
		tbrAddress.setText(user.getAddress());
		tbrResidence.setText(user.getResidence());
		boxbirthdateday.setText(""+user.getBirthdate().getDate());
		boxbirthdatemonth.setText(""+(user.getBirthdate().getMonth()+1));
		boxbirthdateyear.setText(""+(user.getBirthdate().getYear()+1900));
		lbrEmail.setText(user.getEmail());
		lbrCredits.setText(user.getCredits()+" Credits");
		if (user.isMayoffer()) {
			lbrMayOffer.setText("Ja");
		} else {
			lbrMayOffer.setText("Nee");
		}
		if (user.isMaybid()) {
			lbrMayBid.setText("Ja");
		} else {
			lbrMayBid.setText("Nee");
		}
		//completing panels
		p.add(table);
		
		return p;
	}
}
